package com.example.kakaotalk;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class FriendsListFragment extends Fragment implements
		LoaderCallbacks<Cursor> {

	private SimpleCursorAdapter mAdapter;

	private Uri peopleURI = ContactsContract.Contacts.CONTENT_URI;
	private String[] projection = { ContactsContract.Contacts._ID,
			ContactsContract.Contacts.DISPLAY_NAME };
	private String selection = "((" + ContactsContract.Contacts.DISPLAY_NAME
			+ " NOTNULL) AND (" + ContactsContract.Contacts.DISPLAY_NAME
			+ " != '' ))";
	private String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
			+ " COLLATE LOCALIZED ASC";


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_friend_list, container,false);
		ListView lvFriendsListView = (ListView) v.findViewById(R.id.friends_listview);		
		
		lvFriendsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent chatActivityIntent = new Intent(getActivity(),
						ChatActivity.class);
				chatActivityIntent.putExtra("userName", arg0.getItemIdAtPosition(arg2));
				startActivity(chatActivityIntent);

			}
		});

		mAdapter = new SimpleCursorAdapter(getActivity(),
				android.R.layout.simple_list_item_1, null,
				new String[] { ContactsContract.Contacts.DISPLAY_NAME },
				new int[] { android.R.id.text1 }, 0);

		lvFriendsListView.addHeaderView(getSearchBox());
		lvFriendsListView.setAdapter(mAdapter);

		getActivity().getSupportLoaderManager().initLoader(0, null, this);
		return v;
	}

	private View getSearchBox() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		View v = inflater.inflate(R.layout.search_box, null, false);
		EditText etSearch = (EditText)v.findViewById(R.id.friend_list_search);
		etSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				Bundle b = new Bundle();
				b.putString("keyword", s.toString());
				getActivity().getSupportLoaderManager().restartLoader(0, b, FriendsListFragment.this);				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {				
				
			}
		});
		
		return v;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int code, Bundle args) {

		String keyword = null;
		if (args != null)
			keyword = args.getString("keyword");

		Uri baseUri;
		if (keyword != null && !keyword.equals("")) {
			baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(keyword));
		} else {
			baseUri = peopleURI;
		}
		return new CursorLoader(getActivity(), baseUri, projection, selection, null, sortOrder);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		mAdapter.swapCursor(arg1);

	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		mAdapter.swapCursor(null);
	}
}
